/*
	工单产品新增或更新时:
	1、从产品中取出价格设置到工单产品
	2、查询最近一次的工单产品价格设置到上次执行价格上
*/
public without sharing class WorkOrderProductPriceHandler implements Triggers.Handler {
    public void handle() {
        if(Trigger.isBefore && (Trigger.isUpdate || Trigger.isInsert)){
        	List<WorkOrderProduct__c> updList = new List<WorkOrderProduct__c>();
        	List<String> productList = new List<String>();
        	List<WorkOrderProduct__c> wopList = (List<WorkOrderProduct__c>)Trigger.new;
        	Map<Id,WorkOrderProduct__c> mapOld = (Map<Id,WorkOrderProduct__c>)Trigger.oldMap;
            
            Map<Id,String> ownerMap = new Map<Id,String>();
            Map<Id,Id> woMap = new Map<Id,Id>();
            for (WorkOrderProduct__c wop : wopList) {
                woMap.put(wop.WorkOrder__c, null);

            }
            for(WorkOrder__c wo: [select Id, CreatedById from WorkOrder__c where Id in:woMap.keySet() ]){
                woMap.put(wo.Id,wo.CreatedById);
                ownerMap.put(wo.CreatedById,null);

            }

        	for(WorkOrderProduct__c wop : wopList){
        		if(Trigger.isInsert){
        			updList.add(wop);
        			productList.add(wop.Product__c);
        			//查询最近一次工单产品的【本次议价价格】设置到【上次执行价格】上
                    try{
            			WorkOrderProduct__c last = [SELECT Id,ThisNegoPrice__c 
            										FROM WorkOrderProduct__c 
            										WHERE Product__c =: wop.Product__c 
            										AND WorkOrder__r.Account__c =: wop.WorkOrder__r.Account__c 
            										ORDER BY Createddate DESC LIMIT 1];
            			if(last != null){
            				wop.SalesPrice__c = last.ThisNegoPrice__c;
            			}
                    }catch(Exception ex){}
        		}else if(wop.Product__c != mapOld.get(wop.Id).Product__c){
        			updList.add(wop);
        			productList.add(wop.Product__c);
        			//查询最近一次工单产品的【本次议价价格】设置到【上次执行价格】上
                    try{
        			    WorkOrderProduct__c last = [SELECT Id,ThisNegoPrice__c 
        										FROM WorkOrderProduct__c 
        										WHERE Product__c =: wop.Product__c 
        										AND WorkOrder__r.Account__c =: wop.WorkOrder__r.Account__c 
        										AND Id !=: wop.Id 
        										ORDER BY Createddate DESC LIMIT 1];
            			if(last != null){
            				wop.SalesPrice__c = last.ThisNegoPrice__c;
            			}
                    }catch(Exception ex){}
        		}
        	}
        	//从产品中取出价格设置到工单产品
            set<String> companys = new Set<String>();
           for(User u:[Select Id,SubCompany__c from User where Id =:ownerMap.keySet()]){
                ownerMap.put(u.Id,u.SubCompany__c);
                companys.add(u.SubCompany__c);
           }
        	Map<String,Decimal> mapProds = new Map<String,Decimal>();
            for(Price__c p:[SELECT Id,MarketPrice__c,Product__c,Company__c FROM Price__c WHERE Id In :productList and Company__c in:companys]){
                mapProds.put(p.Product__c + '-'+p.Company__c, p.MarketPrice__c);
            }


        	for(WorkOrderProduct__c wop : updList){
                if(woMap.get(wop.WorkOrder__c) != null  && 
                ownerMap.get(woMap.get(wop.WorkOrder__c)) != null){
        			if(mapProds.containsKey(wop.Product__c +'-' + ownerMap.get(woMap.get(wop.WorkOrder__c))) != null )wop.UnitPrice__c = mapProds.get(wop.Product__c +'-' + ownerMap.get(woMap.get(wop.WorkOrder__c)));
        		}
        	}
        }
    }
}